'''
This application process the previous got CSV and plot a better data
'''
import os, random, datetime, subprocess, time
import sys
import csv


def ProcTraceData():
    processed = []
    reader = csv.reader(open('trace.csv', 'r'))
    reader.next()
    for t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 in reader:
        if t2 == '1000' or t4 == '1000' or t6 == '1000' or t8 == '1000':
            continue
        elif t2 == t4 and t4 == t6 and t6 == t8:
            continue
        else:
            print t2, t3, t4, t5, t6, t7, t8, t9
            processed.append([float(t2), float(t3), float(t4), float(t5), float(t6), float(t7), float(t8), float(t9)])
    #end for
    # save file
    with open('./proc.trace.txt', 'w') as f:
        for t2, t3, t4, t5, t6, t7, t8, t9 in processed:
            opt = "%f,%f,%f,%f,%f,%f,%f,%f\r\n" % (t2, t3, t4, t5, t6, t7, t8, t9)
            f.write(opt)
    print "written"
    
    # classify the data into several groups.
    clsd = dict()
    clsdc = dict()
    for t in processed:
        idx = int(t[0] / 100)
        if idx not in clsd:
            clsd[idx] = t
            clsdc[idx] = 1
        else:
            clsd[idx] = [(x + y) for x, y in zip(t, clsd[idx])]
            clsdc[idx] += 1
        #end if
    #end for
    print clsdc
    # get the mean value
    
    for k, v in clsd.items():
        for i in range(0, len(v)):
            clsd[k][i] = clsd[k][i] / clsdc[k]

    print clsd
    
    with open('./result.trace.txt', 'w') as f:
        for k, v in clsd.items():
            opt = "%d,%f,%f,%f,%f,%f,%f,%f,%f\r\n" % (k, v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
            f.write(opt)
    print "completed"
    
    
    
def ProcTestbedData(fname):
    processed = []
    reader = csv.reader(open('testbed.' + fname + '.csv', 'r'))
    reader.next()
    for t0, t1, t2, t3, t4, t5, t6, t7 in reader:
        if t2 == '1000' or t4 == '1000' or t6 == '1000':
            continue
        elif t2 == t4 and t4 == t6:
            continue
        else:
            print t2, t3, t4, t5, t6, t7
            processed.append([float(t2), float(t3), float(t4), float(t5), float(t6), float(t7)])
    #end for
    # save file
    with open('./proc.' + fname + '.txt', 'w') as f:
        for t2, t3, t4, t5, t6, t7 in processed:
            opt = "%f,%f,%f,%f,%f,%f\r\n" % (t2, t3, t4, t5, t6, t7)
            f.write(opt)
    print "written"
    
    # classify the data into several groups.
    clsd = dict()
    clsdc = dict()
    for t in processed:
        idx = int(t[0] / 100)
        if idx not in clsd:
            clsd[idx] = t
            clsdc[idx] = 1
        else:
            clsd[idx] = [(x + y) for x, y in zip(t, clsd[idx])]
            clsdc[idx] += 1
        #end if
    #end for
    print clsdc
    # get the mean value
    
    for k, v in clsd.items():
        for i in range(0, len(v)):
            clsd[k][i] = clsd[k][i] / clsdc[k]

    print clsd
    
    with open('./result.' + fname + '.txt', 'w') as f:
        for k, v in clsd.items():
            opt = "%d,%f,%f,%f,%f,%f,%f\r\n" % (k, v[0],v[1],v[2],v[3],v[4],v[5])
            f.write(opt)
    print "completed"
    
    
def main():
    print "hello";
    #ProcTraceData()
    ProcTestbedData('dense');
    ProcTestbedData('sparse')


if __name__ == "__main__":
    start = time.clock()
    random.seed(1234)
    main()
    elapsed = (time.clock() - start)
    print "Time used: %.2f" % elapsed